package com.deyuanyun.pic.ctrl.validatefun;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.ctrl.annotation.DbTable;
import com.deyuanyun.pic.ctrl.domain.*;
import com.deyuanyun.pic.ctrl.enumeration.AcInterferenceSourceTypeEnum;
import com.deyuanyun.pic.ctrl.enumeration.InterferenceSourceTypeEnum;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 获取交直流干扰源ID
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/9/19）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class DisturbSource implements ValidateInterface {
    @Override
    public <T, E, C> boolean apply(T t, E e, C c) {
        Object obj= c;
        DbTable table=obj.getClass().getAnnotation(DbTable.class);
        Field f=(Field) e;
        String val= (String) t;
        EQPTBaseService service= SpringAppliction.getBean(EQPTBaseService.class);
        try {
            Class deviceClass=getDeviceType(table.excelName(),obj);
            if(deviceClass != null)
            {
                Object device=service.queryObjectByCustomNumber(deviceClass,val);
                String id= BeanUtils.getSimpleProperty(device,"id");
                BeanUtils.setProperty(c,f.getName(),id);
            }else{
                BeanUtils.setProperty(c,f.getName(),"");
            }
            BeanUtils.setProperty(c,"customNumber",val);
            return true;
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }
        return false;
    }

    @Override
    public String getName() {
        return null;
    }


    /**
     * 获取干扰源类型
     *  根据导入excel名称，判断是直流或者交流干扰数据，
     *  然后根据干扰源类型 获取对应的class 类
     * @param name
     * @param obj
     * @return
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    public static  Class getDeviceType(String name,Object obj) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {


        if(name.indexOf("交流干扰监测记录表") >= 0 )
        {
            String type=BeanUtils.getSimpleProperty(obj,"acinterfrencesourcetype");
            if("高压交流输电系统".equals(type))
            {
                return InfluenceHtac.class;
            }else if("交流电气化铁路系统".equals(type)){
                return InfluenceAcer.class;
            }else if("其他交流用电设施".equals(type)){

            }

        }else if(name.indexOf("直流干扰监测记录表") >= 0 ) {
            String type=BeanUtils.getSimpleProperty(obj,"dcinterferencesourcetype");
            if("高压直流输电系统".equals(type))
            {
                return InfluenceDcer.class;
            }else if("直流牵引系统".equals(type)){
                return InfluenceActow.class;
            }else if("其他阴极保护系统".equals(type)){
                return InfluenceCpOther.class;
            }else if("其他直流用电设施".equals(type)){
                return InfluenceDcother.class;
            }


        }


        return null;
    }
}
